package wstrader.task.warehouse.weighing;

import java.sql.*;
import pc.system.*;
import pc.util.*;
import wstrader.task.*;

public class WNR extends Role
{
  private WN wn = null;
  public WNR(WN owner)
  {
    super(owner);
    this.wn = owner;
  }

  public String getNew_WNR()
  {
    String sql = "SELECT * FROM " + wn.getWN_Table() + " WHERE id = " + wn.id ;
    Record rc = new Record(getSQLCommand().executeQuery(sql));
    String wn_ref_number = rc.isEmpty() ? wn.getNew_WN(): rc.getStringValue("ref_number");
    rc.close();

    sql = "SELECT MAX(ref_number) AS ref_number FROM weight_note_ref WHERE wn_id = " + wn.id;
    rc = new Record(getSQLCommand().executeQuery(sql));
    String ref_number = rc.getString("ref_number");
    rc.close();
    if (ref_number == null) ref_number = "";
    int p = ref_number.lastIndexOf('-');
    int no = Utils.strToInt(p > 0 ? ref_number.substring(p+1) : "0") + 1;
    return wn_ref_number + "-" + Utils.formatNumber(no,2);
  }

  public ResultSet getWNR_Detail()
  {
    String sql =
        "SELECT * FROM weight_note_ref " +
        "WHERE type = '" + wn.getType() + "' AND wn_id = " + wn.id;
    return getSQLCommand().executeQuery(sql);
  }

  public void sum_WNR2WN()
  {
    String sql =
        "SELECT SUM(no_of_bags)   AS no_of_bags, " +
        "       SUM(gross_weight) AS gross_weight, " +
        "       SUM(tare_weight)  AS tare_weight,  " +
        "       SUM(net_weight)   AS net_weight " +
        "FROM weight_note_ref " +
        "WHERE type = '" + wn.getType() + "' AND wn_id = " + wn.id;
    Record rc = new Record(getSQLCommand().executeQuery(sql));
    if (rc.isEmpty()) return;
    Table tab = new Table(this,wn.getWN_Table());
    tab.addParam("no_of_bags",   rc.getInt("no_of_bags"));
    tab.addParam("gross_weight", rc.getFloat("gross_weight"));
    tab.addParam("tare_weight",  rc.getFloat("tare_weight"));
    tab.addParam("net_weight",   rc.getFloat("net_weight"));
    tab.update(wn.id);
  }

  public long save_WNR(int no)
  {
    //if (id < 0)  id = save_WN();
    //int no = getRequest().getIntValue("no");
    long wnr_id = getRequest().getLongValue("wnr_id_" + no);
    if (wnr_id == 0) return 0;
    String ref_number  = getRequest().getStringValue("wnr_ref_number_" + no);
    float gross_weight = getRequest().getFloatValue("wnr_gross_weight_" + no);
    float tare_weight  = getRequest().getFloatValue("wnr_tare_weight_" + no);
    float net_weight   = gross_weight - tare_weight;
    int no_of_bags     = getRequest().getIntValue("wnr_no_of_bags_" + no);
    String date        = getRequest().getDateValue("wnr_date_" + no);
    String time        = getRequest().getTimeValue("wnr_time_" + no);

    Table tab = new Table(this,"weight_note_ref");

    tab.addParam("ref_number",   ref_number);
    tab.addParam("wn_id",        "" + wn.id);
    tab.addParam("type",         "" + wn.getType());
    tab.addParam("gross_weight", "" + gross_weight);
    tab.addParam("tare_weight",  "" + tare_weight);
    tab.addParam("net_weight",   "" + net_weight);
    tab.addParam("no_of_bags",   "" + no_of_bags);
    tab.addParam("date", date);
    tab.addParam("time", time);

    wnr_id = tab.update(wnr_id);
    return wnr_id;
  }

  public void save_WNR()
  {
    for (int i = 1; ; i++) {
      long wnr_id = getRequest().getLongValue("wnr_id_" + i);
      if (wnr_id == 0) break;
      save_WNR(i);
    }
  }

  public boolean delete_WNR()
  {
    String sql = "DELETE FROM weight_note_ref WHERE wn_id = " + wn.id;
    return getSQLCommand().executeUpdate(sql) > 0;
  }

  public boolean delete_WNR(int no)
  {
    long wnr_id = getRequest().getLongValue("wnr_id_" + no);
    String sql = "DELETE FROM weight_note_ref WHERE id = " + wnr_id;
    return getSQLCommand().executeUpdate(sql) > 0;
  }


}
